
from flask import render_template, request, redirect, url_for, flash
from project.models import Student

def configure_routes(app):
    @app.route('/')
    def student_list():
        students = Student.query.all()
        return render_template('student_list.html', students=students)

    @app.route('/add_student', methods=['GET', 'POST'])
    def add_student():
        if request.method == 'POST':
            sno = request.form['sno']
            # 检查学号是否已存在
            existing_student = Student.query.filter_by(Sno=sno).first()
            if existing_student:
                flash('该学号已存在，请重新输入')
                return redirect(url_for('add_student'))
            sname = request.form['sname']
            ssex = request.form['ssex']
            try:
                sage = int(request.form['sage'])
                if sage < 10 or sage > 100:
                    flash('年龄应在10到100之间')
                    return redirect(url_for('add_student'))
            except ValueError:
                flash('年龄必须是整数')
                return redirect(url_for('add_student'))
            dno = request.form['dno']
            sclass = request.form['sclass']
            address = request.form['address']
            new_student = Student(Sno=sno, Sname=sname, Ssex=ssex, Sage=sage, Dno=dno, Sclass=sclass, address=address)
            db.session.add(new_student)
            try:
                db.session.commit()
                flash('学生信息添加成功')
                return redirect(url_for('student_list'))
            except Exception as e:
                db.session.rollback()
                flash(f'添加学生信息时出现错误: {str(e)}')
                return redirect(url_for('add_student'))
        return render_template('add_student.html')

    @app.route('/edit_student/<string:sno>', methods=['GET', 'POST'])
    def edit_student(sno):
        student = Student.query.get(sno)
        if request.method == 'POST':
            student.Sname = request.form['sname']
            student.Ssex = request.form['ssex']
            try:
                student.Sage = int(request.form['sage'])
                if student.Sage < 10 or student.Sage > 100:
                    flash('年龄应在10到100之间')
                    return redirect(url_for('edit_student'))
            except ValueError:
                flash('年龄必须是整数')
                return redirect(url_for('edit_student'))
            student.Dno = request.form['dno']
            student.Sclass = request.form['sclass']
            student.address = request.form['address']
            try:
                db.session.commit()
                flash('学生信息修改成功')
                return redirect(url_for('student_list'))
            except Exception as e:
                db.session.rollback()
                flash(f'修改学生信息时出现错误: {str(e)}')
                return redirect(url_for('edit_student'))
        return render_template('edit_student.html', student=student)

    @app.route('/delete_student/<string:sno>')
    def delete_student(sno):
        student = Student.query.get(sno)
        db.session.delete(student)
        try:
            db.session.commit()
            flash('学生信息删除成功')
            return redirect(url_for('student_list'))
        except Exception as e:
            db.session.rollback()
            flash(f'删除学生信息时出现错误: {str(e)}')
            return redirect(url_for('delete_student'))